/*
 * 类名称:AdminOrderBenefitServiceImpl.java
 * 包名称:com.unimall.graded.admin.api.orderbenefit
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-03-17 09:33:11        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.unimall.graded.admin.api.orderbenefit;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.unimall.graded.data.domain.OrderBenefitDO;
import com.unimall.graded.data.mapper.OrderBenefitMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Admin订单分润ServiceImpl实现类
 *
 * @author lin
 * @date 2020-03-17 09:33:11
 */
@Service
public class AdminOrderBenefitServiceImpl implements AdminOrderBenefitService {

    @Autowired
    private OrderBenefitMapper orderBenefitMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer create(Long adminId, OrderBenefitDO orderBenefitDO) throws ServiceException{
        if (orderBenefitDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
          if( orderBenefitDO.getCustomerId() == null ){
            throw new AdminServiceException("不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( orderBenefitDO.getUnimallOrderId() == null ){
            throw new AdminServiceException("不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
          if( orderBenefitDO.getLinkId() == null ){
            throw new AdminServiceException("推广链接id不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( orderBenefitDO.getCustomerBenefit() == null ){
            throw new AdminServiceException("分润金额，单位分不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( orderBenefitDO.getStatus() == null ){
            throw new AdminServiceException("账号状态[0:已支付,1:完成,2:未完成退款,3:已完成退款]不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
  
        Date now = new Date();
        orderBenefitDO.setId(null);
        orderBenefitDO.setGmtCreate(now);
        orderBenefitDO.setGmtUpdate(now);
        return orderBenefitMapper.insert(orderBenefitDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long adminId,Long id) throws ServiceException{
        return orderBenefitMapper.delete(new EntityWrapper<OrderBenefitDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(Long adminId,OrderBenefitDO orderBenefitDO) throws ServiceException{
        if (orderBenefitDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
               if( orderBenefitDO.getCustomerId() == null ){
                throw new AdminServiceException("不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( orderBenefitDO.getUnimallOrderId() == null ){
                throw new AdminServiceException("不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
                if( orderBenefitDO.getLinkId() == null ){
                throw new AdminServiceException("推广链接id不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( orderBenefitDO.getCustomerBenefit() == null ){
                throw new AdminServiceException("分润金额，单位分不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( orderBenefitDO.getStatus() == null ){
                throw new AdminServiceException("账号状态[0:已支付,1:完成,2:未完成退款,3:已完成退款]不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
  
        Date now = new Date();
        orderBenefitDO.setGmtUpdate(now);
        return orderBenefitMapper.updateById(orderBenefitDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrderBenefitDO info(Long adminId,Long id) throws ServiceException{

        if(id == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }

        return orderBenefitMapper.selectById(id);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<OrderBenefitDO> list(Long adminId,
Long id,
Long customerId,
Long unimallOrderId,
String userId,
 Integer page,
         Integer limit) throws ServiceException{

        Wrapper<OrderBenefitDO> wrapper = new EntityWrapper<OrderBenefitDO>();
        wrapper.orderBy("id", false);

            if( id != null ){
            wrapper.eq("id", id);
        }
            if( customerId != null ){
            wrapper.eq("customer_id", customerId);
        }
            if( unimallOrderId != null ){
            wrapper.eq("unimall_order_id", unimallOrderId);
        }
            if( !StringUtils.isEmpty(userId) ){
            wrapper.eq("user_id", userId);
        }
                                            
        List<OrderBenefitDO> OrderBenefitDOs = orderBenefitMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = orderBenefitMapper.selectCount(wrapper);
        return new Page<OrderBenefitDO>(OrderBenefitDOs, page, limit, count);
    }
}